import copy

import pygame


class BlockAction:
    # 绘制所有方块,自动排序
    def drawAll(block_list,re,screen):
        # block_list.text进行排序处理
        text_List = []
        for block in block_list:
            if block is not None:
                text_List.extend([ord(char) for char in block.text])
        text_List.sort();
        if re == -1:
            block_list.reverse()
            text_List.reverse()
        if len(text_List)>0:
            for index,block in enumerate(block_list):
                block.setText(pygame,str(chr(text_List[index])))
        for block in block_list:
            if block is not None:
                block.draw(screen,pygame)

    # 绘制所有方块，无排序
    def drawAllNoOrder(block_list,screen):
        for block in block_list:
            block.draw(screen,pygame)
    # 移动list所有方块
    def moveAll(stepx,stepy,block_list,keyEvent,block_last):
        # global block_last
        # if block_last == None or len(block_last)==0:
        #     block_last = []
        #     block_last.append(1)
        block_last[0] = copy.copy(block_list[len(block_list)-1])
        block_start = block_list[0]
        block_end = copy.copy(block_list.pop())
        new_x = 0
        new_y = 0
        if keyEvent == "left":
            new_x = -1
        if keyEvent == "right":
            new_x = 1
        if keyEvent == "up":
            new_y = -1
        if keyEvent == "down":
            new_y = 1
        block_end.x = block_start.x + new_x * block_start.width
        block_end.y = block_start.y + new_y * block_start.height
        block_list.insert(0,block_end)
        # block_end.move(stepx,stepy)
        # for block in block_list:
        #     block.move(stepx,stepy)

    # 自动移动
    def moveAuto(self,block_new,speed,screen):
        self.moveAutoX(block_new,speed,screen)
        self.moveAutoY(block_new,speed,screen)


    # x方向上自动移动
    def moveAutoX(block_new,speed,screen):
        # print("x位置：", block_new.x,",屏幕宽度", screen.get_width(),"", block_new.is_x_left,"y位置：", block_new.y,",屏幕高度", screen.get_height(),"", block_new.is_y_donw,"block_flower_group:",
        #       len(block_flower_group))
        if block_new.is_x_left:
            if block_new.x -speed >0:
                block_new.x-= speed
            else:
                block_new.is_x_left = False
        else:
            if block_new.x+speed <screen.get_width()-block_new.width:
                block_new.x+=speed
            else:
                block_new.is_x_left = True

    def moveAutoY(block_new,speed,screen):
        # y轴移动
        # print("x位置：", block_new.x,",屏幕宽度", screen.get_width(),"", block_new.is_x_left,"y位置：", block_new.y,",屏幕高度", screen.get_height(),"", block_new.is_y_donw,"block_flower_group:",
        #       len(block_flower_group))
        if block_new.is_y_donw:
            if block_new.y -speed >0:
                block_new.y-= speed
            else:
                block_new.is_y_donw = False
        else:
            if block_new.y + speed <screen.get_height()-block_new.height:
                block_new.y+=speed
            else:
                block_new.is_y_donw = True

    @classmethod
    def checkCollision(cls, block_bee_group, block_flow_group):
        # 定义map，存储key value
        result = {}
        # 碰撞检测
        # block_score = [ x for x in block_flow_group if pygame.Rect(block_bee_group[0].show()).colliderect(pygame.Rect(x.show())) ]
        block_bee_sc = []
        block_flow_sc = []
        for block_bee in block_bee_group:
            for block_flower in block_flow_group:
                if pygame.Rect(block_bee.show()).colliderect(pygame.Rect(block_flower.show())):
                    block_bee_sc.append(block_bee)
                    block_flow_sc.append(block_flower)

        result["block_bee_sc"] = block_bee_sc
        result["block_flow_sc"] = block_flow_sc
        return result

